/*
 * PriNetApi.h
 *
 *  Created on: 2017-6-23
 *      Author: root
 */

#ifndef PRINETAPI_H_
#define PRINETAPI_H_
//供内部使用的头文件
//不应出现在其他头文件中
#include"PublicConfig.h"
#include"WorkPingApi.h"
#include"WorkConnectApi.h"
#include"PublicSocket.h"
#include"PublicTool.h"
#define USE_PUBLIC_SOCKET 1
#if USE_PUBLIC_PING 
//PING相关函数
typedef union{
u_short seqex;//发送SEQ
struct {
u_short  check:4;//校验字段同一进程中不同地方调用
u_short  id:12;  //跳转主
}flag;
}sequn;
typedef struct _IPPINGCHECK{
char     ip[40];//IP兼容v6
sequn    seqinfo;
struct  timeval sendtime;//发送时间
bool operator < (struct _IPPINGCHECK const &other)const{
    if(seqinfo.seqex<other.seqinfo.seqex)
        return true;
    if(seqinfo.seqex==other.seqinfo.seqex){
        //return strcmp(ip,other.ip)<0;
        return memcmp(ip,other.ip,40)<0;
        //return strncmp(ip,other.ip,40)<0;
    }
    return false;
}
}IPPINGCHECK;
typedef std::set<IPPINGCHECK>SETIPPINGCHECK;
//ping间隔us
#define DEFAULT_PING_INTERVAL     5000
//ping单次发送数量
#define DEFAULT_PING_INTERNUM     20
//ping超时ms
#define DEFAULT_PING_TIMEOUT      2000
//构建队列间隔ms
#define DEFAULT_PING_INTERMAKE    1000
//DataSize包含包头8字节，DataSize至少28字节
int MakePingPacket(char*Packet,int DataSize,sequn seqinfo,const char*dstip,struct timeval*timestampval);
//大于0返回耗时,-2包不是本程序的包,-1其他错误,0ttl超时,seqinfo不为空
int ReadPingPacket(char*Packet,int DataSize,struct timeval timestamp,u_char check,sequn*seqinfo,char*dstip,char*fromip);
int ReadPingPacket6(char*Packet,int DataSize,struct timeval timestamp,u_char check,sequn*seqinfo,char*dstip,char*fromip);
//0成功 -1失败
int SendICMPPacket(CPublicSocket*sock,int PacketSize,IPPINGCHECK&checkinfo,int ttl=0);
//-2 无关，-1其他错误，0 ttl，其他耗时
int ReadICMPPacket(CPublicSocket*sock,int checkid,IPPINGCHECK&checkinfo,struct timeval&timestamp,char*fromip);
#endif

#if USE_PUBLIC_CONNECT
/////端口相关函数
//系统信息
typedef struct _ConnectSysInfo{
int     fd;//使用套接字
timeval timestart;//开始连接时间
}ConnectSysInfo;
typedef std::map<CONNECTKEY,ConnectSysInfo>     MAPCONNECTWAITINFO;   //IP 等待信息
#endif

///UDP相关函数
#if USE_PUBLIC_UDP
int CreateICMPSocketUDP(int INTERVAL_TIME);
int CreateUDPSocket(int INTERVAL_TIME,const char*SRC_IP);
int ReadICMPUDPPacket(int icmp_sock,CONNECTKEY*key);
int ReadPingUDPPacket(char*Packet,int DataSize,CONNECTKEY*key);
#endif
#endif /* PRINETAPI_H_ */
